const { adminsModel } = require('./models/adminsModel');
const { complaintsModel } = require("./models/complaintsModel")
const { lessorsModel } =require("./models/lessorsModel")
const { OrdersModel } = require("./models/ordersModel")
const { tenantsModel } = require("./models/tenantsModel")
const { parkingsModel } = require("./models/parkingsModel")
//普通管理员登录
module.exports.AdminsLogin = async function (username) {
    const data = await adminsModel.find(username)
    return data
}
//超级管理员登录
module.exports.SuperAdminsLogin3 = async function (msg) {
  const {username,password}=msg;
    const data = await adminsModel.find({username:username,password:password,power:1})
    return data
}
//新增管理员
module.exports.AddAdmins=async (msg)=>{
  const data=await adminsModel.create(msg)
  return data
}
//获取管理员数据
module.exports.getAdmins=async (cha,currentPage, pageSize)=>{
  // console.log(cha,currentPage, pageSize);
  let total = await adminsModel.countDocuments();//获取数据总条数
  const chao = await adminsModel.find({power: 1});//超管数据
  total= total- chao.length
  const pages = Math.ceil(total/pageSize);//数据总页数
  const data=await adminsModel.find(cha)
  .limit(pageSize)//设置请求数据条数
  .skip((currentPage-1)*pageSize)//设置跳过数据条数
  // console.log('3',data);
  return {data,total,pages}
}
//修改管理员信息
module.exports.setAdmins=async(msg)=>{
  const _id = msg._id
  const data = await adminsModel.updateOne({_id},msg)
  return data
}

module.exports.getData=async function(){
    const lessorsNum = (await lessorsModel.find()).length
    const complaintsNum = (await complaintsModel.find()).length
    const tenantsNum = (await tenantsModel.find()).length
    const ordersNum = (await OrdersModel.find()).length
    return{lessorsNum,complaintsNum,tenantsNum,ordersNum} 
}
module.exports.getData2=async function(){
    const arr = await parkingsModel.find()
    const notReviewed = arr.filter(item => item.state==1).length
    const leased = arr.filter(item=>item.state==5).length
    const notLeased = arr.filter(item=>item.state==2||item.state==4).length
    return {notReviewed,leased,notLeased}
}

module.exports.getData3=async function(){
    const arr = await OrdersModel.find()
    const notPay = arr.filter(item => item.state==1).length
    const isPay=arr.filter(item=>item.state==2).length
    const cancel=arr.filter(item=>item.state ==3).length
    const complete=arr.filter(item=>item.state==4).length
    return {
        notPay,
        isPay,
        cancel,
        complete
    }
}